
<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <meta name="author" content="Rainy" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="renderer" content="webkit" />
    <link rel="shortcut icon" href="static/SmartWiki/favicon.ico">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1.0, user-scalable=0">
    
    <title>仓库列表 - MxsDoc</title>
	<link href="css/logo/logo.css" rel="stylesheet">
    <!-- CSS: Bootstrap、font-awesome、boostrapQ、 -->
    <link href="static/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="static/font-awesome/css/font-awesome.min.css" rel="stylesheet">
    <link href="static/styles/styles.css" rel="stylesheet">
    <link href="static/zTree/css/metroStyle/standardStyle.css" rel="stylesheet" id="zTreeCss">
	<link href="static/pagination/pagination.css" rel="stylesheet" >
	<script src="static/scripts/jquery.min.js"></script>
	<!-- this style define a lots css for project.html 
	<link rel="stylesheet" type="text/css" href="css/style.css">
    -->
    
    <style>
        /* 大图标 */
        .bigicon {
        	background-size:40px 40px !important;
		    width: 40px;
		    height: 40px;
        }

        /* 小图标 */
        .smallicon {
            background-size:20px 20px !important;
            width: 20px;
            height: 20px;
        }
    </style>
</head>
<body>
<div class="manual-reader">
    <header class="navbar navbar-static-top smart-nav navbar-fixed-top" role="banner">
        <div class="container">
            <div class="navbar-header">
                <a class="navbar-brand"  style="color: #445;font-weight: 600;" href="/DocSystem">
                	<i class="logo small"></i> 
                	MxsDoc
                </a>
                <div class="searchbar pull-left visible-lg-inline-block visible-md-inline-block">
                    <div class="form-inline">
                        <input class="form-control" id="searchWord" name="searchWord" type="search" placeholder="请输入关键词..." oninput="$('[id=\'searchWord\']:eq(1)').val(this.value)">
                        <button class="search-btn" onclick="searchDoc();">
                            <i class="fa fa-search"></i>
                        </button>
                    </div>
                </div>
                <div class="btn-group dropdown-menu-right pull-right slidebar visible-xs-inline-block">
                    <button class="btn btn-default dropdown-toggle" type="button" data-toggle="dropdown"><i class="fa fa-align-justify"></i></button>
                    <ul class="dropdown-menu" role="menu">
						<li id="mobileNewReposBtn"  style="display:none">
							<a href="#" title="新建仓库" onclick="newRepos()">新建仓库</a>
						</li>
						<li id="mobileGoManagerBtn" style="display:none">
							<a href="/DocSystem/manager/main.html" title="管理后台">管理后台</a>
						</li>
						
                        <li id="mobileLoginBtn">
                            <a onclick="showLoginPanel()" title="用户登录">登录</a>
                        </li>
                        <li id="mobileLogoutBtn"  style="display:none">
                            <a onclick="userLogout()" title="退出登录">退出登录</a>
                        </li>
                    </ul>
                </div>
             </div>

            <nav class="collapse navbar-collapse bs-navbar-collapse" role="navigation">
                <ul class="nav navbar-nav navbar-right">
				    <li id="userImgDiv" style="display:none"><img id="userImg" name="userImg" width="40px" height="40px" src="images/default/defaultHeadPic.png" onerror="UserImgErrHandler();" class="img-circle m5"/></li>
					<li id="userInfoDiv" style="display:none">
							<a href="" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><span>Rainy</span><span class="caret"></span></a>
							<ul class="dropdown-menu" role="menu">
								<li><a href="/DocSystem/web/myHostPage.html">个人中心</a></li>
								<li><a href="/DocSystem/web/shareManager.html">我的分享</a></li>
								<li><a href="/DocSystem/web/reposManager.html">仓库管理</a></li>
								<li id="logoutBtn"><a onclick="userLogout()">退出登录</a></li>
							</ul>
					</li>
                    <li id="loginBtn">
                        <a onclick="showLoginPanel()"  title="用户登录">登录</a>
                    </li>
                    <li id="newReposBtn" style="display:none">
						<a href="#" title="新建仓库" onclick="newRepos()">新建仓库</a>
					</li>
					<li id="goManagerBtn" style="display:none">
						<a href="/DocSystem/manager/main.html" title="管理后台">管理后台</a>
					</li>
                </ul>
            </nav>
        </div>
    </header>

    <div class="container smart-container">
        <div class="row">
            <div class="searchbar visible-sm-inline-block visible-xs-inline-block" style="width: 95%; margin-left: 10px;">
                <div class="form-inline">
                    <input class="form-control line-search-input" id="searchWord" name="keyword" type="search" placeholder="请输入关键词..." oninput="$('[id=\'searchWord\']:eq(0)').val(this.value)">
                    <button class="line-search-btn" onclick="searchDoc();">
                        <i class="fa fa-search"></i>
                    </button>
                </div>
            </div>
        </div>

        <div class="row">
            <div id="project-box" class="project-box"></div>
            <div class="clearfix"></div>
            <div class="manual-page">
            </div>
        </div>
        <div class="clearfix"></div>
    </div>
    <footer class="footer">
        <div class="container">
            <div class="row text-center">
                <!-- <ul>
                    <li><div id="qrcode"></div></li>
                </ul> -->
                <ul>
                    <li><a href="#">MxsDoc</a></li>
                    <li>&nbsp;·&nbsp;</li>
                    <li><a onclick="showFeebackPanel()">意见反馈</a> </li>
                </ul>
            </div>
        </div>
    </footer>

    <div id="scrollToTopDiv"></div>

    <!-- 利用bootstrap的响应式，判断是否小屏幕 -->
    <div id="visibleOnSmall" class="visible-xs"></div>
</div>

<script type="text/html" id="tmpl-projects">
    <ul>
	{{each list as repos i}}
    	<li>
        	<a href="project.html?vid={{ repos.id }}" class="box" title="" >
            	<div class="pull-left imgbox">
                	<i class="fa fa-desktop"></i>
            	</div>
            	<h4>{{ repos.name }}</h4>
            	<span></span>
        	</a>
        	<p class="summary hidden-xs hidden-sm hidden-md">
            	<a href="project.html?vid={{ repos.id }}" class="text">
                	{{ repos.info }}
            	</a>
        	</p>
    	</li>
    {{/each}}
    	<li>
        	<a href="myPreferLinks.html" class="box" title="" >
            	<div class="pull-left imgbox">
                	<i class="fa fa-star-o"></i>
            	</div>
            	<h4>我的常用网站</h4>
            	<span></span>
        	</a>
        	<p class="summary hidden-xs hidden-sm hidden-md">
            	<a href="myPreferLinks.html" class="text">自定义您的常用网站</a>
        	</p>
    	</li>
    </ul>
</script>

<script type="text/html" id="tmpl-searchResults">
    <div style="margin-top: 20px"></div>
    {{each list as doc i}}
        <div class="s-box">
            <!--<div class="s-img">
                <i class="{{doc.iconStyle}}"></i>
            </div>-->
            <div class="s-content-box">
                <h4 class="s-title">
                    <i class="{{doc.iconStyle}}"></i>
                    <a href="{{doc.link}}" title="" target="_blank">{{# doc.name}}</a>
                </h4>
                <div class="s-content">
                    {{doc.timeInfo}} {{# doc.content}}
                </div>
                <div class="s-footer">
                    <a href="{{doc.link}}" target="_blank">
                        {{ doc.reposName }} :: {{ doc.path }}
                    </a>
                </div>
            </div>

            <hr/>
        </div>
    {{/each}}
    <!-- 搜索结果分页功能 -->
    <div class="wrap">
        <ul id="newsList"></ul>
        <div id="pagination" class="l-page"></div>
    </div>
</script>

<!-- Include all compiled plugins (below), or include individual files as needed -->
<script src="static/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>
<script src="static/scripts/scripts.js" type="text/javascript"></script>
<script src="static/scripts/template.js" type="text/javascript"></script>
<script type="text/javascript" src="static/bootstrapQ/qiao.js"></script>
<script type="text/javascript" src="js/DocSys.js"></script>
<script type="text/javascript" src="js/logon.js"></script>
<script type="text/javascript" src="js/base64.js"></script>
<script type="text/javascript" src="static/pagination/jquery.pagination.js"></script>
<script src="js/qrcode.js"></script>
<script type="text/javascript">
	//页面初始化
	var gDocSysConfig = null;
	var login_user = "";
	var gShareId;
	var gPageIndex = 1;
	var gPageSize = 10;
	
	function pageInit()
    {
    	console.log("pageInit");
    	
		//Set the event handler for keydown
		console.log("pageInit() Set the event handler for keydown");
		document.onkeydown=function(event)
		{
			//浏览器兼容性处理
			var e = event || window.event || arguments.callee.caller.arguments[0];
			EnterKeyListenerForSearchDoc(e);	
		}
		
    	getDocSysConfig();

		// 加载滚动到顶端功能
		$("#scrollToTopDiv").load("goTop.html")
    	 	
    	//确定当前登录用户是否已登录
    	$.ajax({
            url : "/DocSystem/User/getLoginUser.do",
            type : "post",
            dataType : "json",
            data : {},
            success : function (ret) {
                if( "ok" == ret.status )
                {
                	var user = ret.data;
                	login_user = user;
                	//显示用户信息
                	ShowUserInfo(user);
                	loginBtnCtrl(user);
                	
                	//用戶已經登錄才能看到倉庫
                	showReposList();
                }
                else 
                {
                	console.log("pageInit() 获取用户信息失败:" + ret.msgInfo);
                	if(ret.data && ret.data == "needAddFirstAdmin")
                    {
                		showAddFirstAdminUserPanel();
                    }
                	else
                	{
	                	console.log(ret.msgInfo);
	                    //showLoginPanel();
	                	//showErrorMessage("获取用户信息失败:" + ret.msgInfo);
                	}
                }
            },
            error : function () {
                alert("服务器异常:获取用户信息失败");
            }
        });
		
		//showQRCode();
    }
    
	function showQRCode()
	{
		$.ajax({
            url : "/DocSystem/Doc/getLoginLink.do",
            type : "post",
            dataType : "json",
            data : {},
            success : function (ret) {
         		console.log("getLoginLink ret:", ret)
         		if(ret.status == "ok")
            	{
         			var url = ret.data;
         			console.log(url);
         			new QRCode(document.getElementById("qrcode"), url);  // 设置要生成二维码的链接
         		}
                else
                {
                	new QRCode(document.getElementById("qrcode"), "http://dw.gofreeteam.com/DocSystem");  // 设置要生成二维码的链接
                    showErrorMessage("获取登录链接失败:" + ret.msgInfo);
                }
            },
            error : function () {
            	new QRCode(document.getElementById("qrcode"), "http://dw.gofreeteam.com/DocSystem");  // 设置要生成二维码的链接
                showErrorMessage('获取登录链接失败: 服务器异常');
            }
        });

	}
	
    function userLogout()
   	{
    	logout();
    	loginBtnCtrl();
   	}
    
    function loginBtnCtrl(user)
    {
    	if(user)
    	{
        	$('#loginBtn').hide();
        	$('#mobileLoginBtn').hide();
        	
        	$('#logoutBtn').show();
           	$('#mobileLogoutBtn').show();            
           	
        	if(user.type > 0) //超级管理员和管理员才能新建仓库，后台也需要进行控制，普通用户只能建立最多10个私人仓库
        	{
            	$('#newReposBtn').show();
            	$('#mobileNewReposBtn').show();
            	
        		$('#goManagerBtn').show();
        		$('#mobileGoManagerBtn').show();
        	}
    	}
    	else
    	{
        	$('#loginBtn').show();
        	$('#mobileLoginBtn').show();
        	
        	$('#logoutBtn').hide();
           	$('#mobileLogoutBtn').hide();

           	$('#newReposBtn').hide();
        	$('#mobileNewReposBtn').hide();
        	
        	$('#goManagerBtn').hide();
        	$('#mobileGoManagerBtn').hide();        	
    	}
    }
    
	function getDocSysConfig()
    {
    	console.log("getDocSysConfig");
        $.ajax({
            url : "/DocSystem/Repos/getDocSysConfig.do",
            type :"post",
            dataType :"json",
            data : null,
            success : function (ret) {
                if(ret.status == "ok")
                {
                	gDocSysConfig = ret.data;
                	console.log("getDocSysConfig config:", gDocSysConfig);
	            }
	            else
	            {
	            	console.log(ret.msgInfo);
	            }
            },
            error : function () {
            	console.log('服务器异常:获取DocSysConfig失败');
            }
        });
    }

    
    function showReposList()
    {
    	console.log("showReposList");
        $.ajax({
            url : "/DocSystem/Repos/getReposList.do",
            type :"post",
            dataType :"json",
            data : null,
            success : function (ret) {
                if(ret.status == "ok")
                {
	                var html = template('tmpl-projects', {
	                    list : ret.data
	                });
	                
	                $("#project-box").html(html);
	            }
	            else
	            {
	            	console.log(ret.msgInfo);
	            	showErrorMessage("获取仓库列表失败: " + ret.msgInfo);
	            }
            },
            error : function () {
            	showErrorMessage('获取仓库列表失败: 服务器异常');
            }
        });
    }

    $(function () {
    	pageInit();
    });
    
    function newRepos() {
    	showAddReposPanel();
    }
    
    function callBackForAddReposSuccess()
   	{
    	showReposList();
    	
    	MyJquery.closeBootstrapDialog("addRepos");
		
    	//临时方案避免滚动条消失
		//window.location.reload();
   	}
    
    function callbackForCancelAddRepos()
    {
    	MyJquery.closeBootstrapDialog("addRepos");

    	//临时方案避免滚动条消失
		//window.location.reload();    	
    }
    
    
	function showAddReposPanel()
	{
		console.log("showAddReposPanel()");
		bootstrapQ.dialog({
				id: 'addRepos',
				url: 'addRepos.html',
				title: '新建仓库',
				msg: '页面正在加载，请稍等...',
				foot: false,
				big: false,
				//okbtn: "确定",
				callback: function(){
					ReposConfig.addReposPageInit(callBackForAddReposSuccess, callbackForCancelAddRepos);
				},
		});
	}
    
	function showFeebackPanel()
	{
		console.log("showFeebackPanel");
		var href = "https://gitee.com/RainyGao/DocSys/issues";
		window.open(href);   //新窗口打开
		
		/*
		bootstrapQ.dialog({
			url: 'feeback.html',
			title: '意见与反馈',
			msg: '页面正在加载，请稍等...',
			foot: false,
			big: false,
			callback: function(){
				FeebackPageInit();
			},
		});*/
	}
	
	//回车键监听函数，该event已经过兼容性处理 
	function EnterKeyListenerForSearchDoc(event){
		if (event.keyCode == 13)
		{  
			var isFocus=$("#searchWord").is(":focus");  
			if(isFocus)
			{
				console.log("enter key listener for SearchDoc");
				searchDoc();
			}
			return;
	 	}
	}
	
	//注意:page从0开始，pageIndex从1开始
	var searchResults = [];
	function searchDoc(pageIndex)
	{
		var searchWord = $("#searchWord").val();
		var sort = "";
		
       	$.ajax({
               url : "/DocSystem/Doc/searchDoc.do",
               type : "post",
               dataType : "json",
               data : {
                   searchWord:searchWord,
                   sort:sort,
                   //pageIndex: page,
               	   //pageSize: gPageSize
               },
               success : function (ret) {
	           		console.log("searchDoc ret:", ret);
	        		if(ret.status == "ok")
	           		{
	           			console.log("文件搜索成功", ret);
						var docList = ret.data;
						searchResults = docList; //赋值给searchResult,用于分页显示控制
						
						var total = ret.dataEx; //搜索结果总数
				      	for(var i=0; i<docList.length; i++)
				      	{
				           var jsonObj = docList[i];
				           jsonObj.link = getDocLink(jsonObj);
				           
				           //设置用户自定义文件图标（必须在standardStyle中有定义）
				           if(jsonObj.type == 1)
				           {
				        	   var iconType = getDiyFileIconType(jsonObj.name);
				        	   if(iconType && iconType != "")
				        	   {
				        		   jsonObj.iconStyle = "icons smallicon file " + iconType;
				        	   }
				        	   else
				        	   {
				        		   jsonObj.iconStyle = "icons smallicon file";
				        	   }
				           }
				           else
				           {
				        	   jsonObj.iconStyle = "icons smallicon folder";
				           }

                            // 加粗渲染搜索结果
                            jsonObj.name = searchWordsRender(jsonObj.name, searchWord);
                            jsonObj.info = searchWordsRender(jsonObj.info, searchWord);
				       }

				       showSearchResults(searchResults, pageIndex);
	               	}
	               	else
	               	{
	               		showErrorMessage("文件搜索出错:" + ret.msgInfo);
	               	}
               },
               error : function () {
                   showErrorMessage("文件搜索出错:服务器异常");
               }
           });
	}
	
	function showSearchResults(searchResults, pageIndex)
	{
		console.log("showSearchResults pageIndex:" + pageIndex);
		var page = pageIndex ? pageIndex : 0;

		var docList = [];
		var startIndex = page * gPageSize; //当前页第一个搜索结果索引号
		var total = searchResults.length;
		var size = total - startIndex;
		if(size > gPageSize)
		{
			size = gPageSize;
		}
		for(var i=0; i< size; i++)
		{
			docList[i] = searchResults[startIndex+i];
			if(!docList[i].state)
			{
				//do decode the content
				if(docList[i].content)
				{
					docList[i].content = base64_decode(docList[i].content);
					docList[i].content = subByte(docList[i].content, docList[i].content.length);
				}
				docList[i].state = 1;
			}
		}
		console.log(docList);
		
        var html = template('tmpl-searchResults', {
                list : docList,
        });
	    
        $("#project-box").html(html);
        

        // 渲染分页
        $("#pagination").pagination({
             /*当前页码*/
             currentPage: page + 1,
             /*总共有多少页*/
             totalPage: Math.ceil(total/gPageSize),
             /*是否显示首页、尾页 true：显示 false：不显示*/
             isShow:true,
             /*分页条显示可见页码数量, 小屏幕只显示3个页码，大屏幕显示5个*/
             count: onSmallScreen() ? 3 : 5,
             /*第一页显示文字*/
             homePageText:'首页',
             /*最后一页显示文字*/
             endPageText:'尾页',
             /*上一页显示文字*/
             prevPageText:'上一页',
             /* 下一页显示文字*/
             nextPageText:'下一页',
             /*点击翻页绑定事件*/
             callback: function(newPageIndex) {
             	showSearchResults(searchResults, newPageIndex - 1);
             }
        });
        
	}
	
	//截取字符 去除乱码 避免半个中文字符
	function subByte(source, length) {
	    source = String(source);
	    source = source.substr(0,length).replace(/([^\x00-\xff])/g,"\x241 ")//双字节字符替换成两个
	       .substr(0,length)//截取长度
	       .replace(/[^\x00-\xff]$/,"")//去掉临界双字节字符
	       .replace(/([^\x00-\xff]) /g,"\x241");//还原
	    return source;
	};

    /**
     * 加粗加红显示搜索关键字
     * @param str 原始字符串
     * @param keyword 关键字
     */
	function searchWordsRender(str, keyword) {
	    if (str && keyword && str.length > keyword.length) {
	        var keywordLength = keyword.length;
	        var tmpStr = str.toLowerCase();
	        keyword = keyword.toLowerCase();
	        var indexs = [];

	        var startPosition = 0;
	        var index = -1;
	        while ((index = tmpStr.indexOf(keyword, startPosition)) >= 0) {
	            startPosition = index + keyword.length;
	            indexs.push(index);
            }

            if (indexs.length == 0) {
                return str;
            } else {
                // 倒序替换
                for (var i = indexs.length - 1; i >=0; i--) {
                    var start = indexs[i];
                    var end = start + keywordLength;
                    var left = str.substring(0, start);
                    var mid = str.substring(start, end);
                    var right = str.substring(end, str.length);

                    // 关键字加粗显示
                    str = left + "<em>" + mid + "</em>" + right;
                }
                return str;
            }
        } else {
	        return str;
        }

    }

    function onSmallScreen() {
        return $("#visibleOnSmall:visible").size() > 0;
    }

</script>
</body>
</html>